package admin

import (
	"beergo/app/check/models"
	auto "beergo/app/hr/auto/controller"
	"beergo/app/hr/conf"
	"beergo/conf/const/code"
	"beergo/lib/tool"
	"github.com/beego/beego/v2/server/web"
)

type JobAdminController struct {
	auto.JobController
}

func init() {
	web.Router(conf.UrlPrefix + "/Job/:id", 		&JobAdminController{}, "*:GetAction")
	web.Router(conf.UrlPrefix + "/Job/info", 		&JobAdminController{}, "*:InfoAction")
	web.Router(conf.UrlPrefix + "/Job/scan", 		&JobAdminController{}, "*:ScanAction")
	web.Router(conf.UrlPrefix + "/Job/list", 	    &JobAdminController{}, "*:ListAction")
	web.Router(conf.UrlPrefix + "/Job/update", 	&JobAdminController{}, "*:UpdateAction")
	web.Router(conf.UrlPrefix + "/Job/create", 	&JobAdminController{}, "*:CreateAction")
	web.Router(conf.UrlPrefix + "/Job/copy", 	    &JobAdminController{}, "*:CopyAction")
	web.Router(conf.UrlPrefix + "/Job/edit", 	    &JobAdminController{}, "*:EditAction")
	web.Router(conf.UrlPrefix + "/Job/delete", 	&JobAdminController{}, "*:DeleteAction")
	web.Router(conf.UrlPrefix + "/Job/:id/:field", &JobAdminController{}, "*:SonsAction")
	web.Router(conf.UrlPrefix + "/Job/choice", &JobAdminController{}, "*:ChoiceAction")
	web.Router(conf.UrlPrefix + "/Job/category", &JobAdminController{}, "*:CategoryAction")
}
//func (this *JobAdminController) Prepare() {}
func (this *JobAdminController) ListAction() {
	admin:=this.GetAdminUser()
	if admin.Id > 0 {
		this.QueryBuilder.Conditions["company__user__id"] = admin.Id
	}
	lists, err := this.Repository.GetList(this.QueryBuilder)
	if err != nil {
		this.StopError(code.Error, "系统错误", err.Error())
	}
	this.JsonData["lists"] = lists
	this.JsonData["query"] = this.QueryBuilder.FeedBack()
	this.ReturnSuccess("OK", this.JsonData)
}
//func (this *JobAdminController) GetAction() {}
//func (this *JobAdminController) InfoAction() {}
//func (this *JobAdminController) ScanAction() {}
func (this *JobAdminController) CreateAction() {
	entity := this.Model.New()
	_, err := this.JsonParseModel(entity)
	this.AnaliseErrors(err)
	admin:=this.GetAdminUser()
	rep := models.OrgRepository{}
	org:=rep.FindOneBy(tool.M{"User":admin.Id},nil)
	if org.Id > 0 {
		entity.Company = & models.Org{Id: org.Id}
	}

	_, errSave := entity.Save()
	this.AnaliseErrors(errSave)
	this.FillM2M(entity)
	this.ReturnSuccess("OK", entity.Id)
}
//func (this *JobAdminController) UpdateAction() {}
//func (this *JobAdminController) CopyAction() {}
//func (this *JobAdminController) DeleteAction() {}
//func (this *JobAdminController) EditAction() {}
//func (this *JobAdminController) SonsAction() {}
//func (this *JobAdminController) ChoiceAction() {}
//func (this *JobAdminController) CategoryAction() {}
